package bb.lanxing.lib.devices.base.converter;

import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import bb.lanxing.lib.devices.base.CharacteristicConverter;
import bb.lanxing.lib.devices.ble.device.RadarDevice;
import bb.lanxing.lib.devices.core.ble.IGattClient;
import java.util.ArrayList;
import java.util.Collections;

public class RadarConverter implements CharacteristicConverter {
    private final String TAG = "RadarConverter";
    private OnRadarDataListener onRadarDataListener;


    public interface OnRadarDataListener {
        void onRadarData(ArrayList arrayList);
    }

    public void setOnRadarDataListener(OnRadarDataListener onRadarDataListener) {
        this.onRadarDataListener = onRadarDataListener;
    }

    @Override
    public void convert(IGattClient iGattClient, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        if (bluetoothGattCharacteristic.getUuid().equals(RadarDevice.BLE_RADAR_NOTIFY)) {
            byte[] value = bluetoothGattCharacteristic.getValue();
            byte b = value[0];
            int length = (value.length <= 2 || value.length - 2 < 2) ? 0 : (value.length - 2) / 2;
            if (length > 0) {
                ArrayList arrayList = new ArrayList();
                for (int i = 0; i < length; i++) {
                    arrayList.add(new RadarDataBean(value[(RadarDataBean.size * i) + 1] & 255, value[(RadarDataBean.size * i) + 2] & 255, System.currentTimeMillis()));
                }
                Collections.sort(arrayList);
                if (length < 9) {
                    for (int i2 = 0; i2 < 9 - length; i2++) {
                        arrayList.add(RadarDataBean.emptyData());
                    }
                }
                this.onRadarDataListener.onRadarData(arrayList);
                String str = this.TAG;
                Log.d(str, "Received radaData:   " + arrayList.size());
                return;
            }
            OnRadarDataListener onRadarDataListener = this.onRadarDataListener;
            if (onRadarDataListener == null) {
                return;
            }
            onRadarDataListener.onRadarData(new ArrayList());
        }
    }


    public static class RadarDataBean implements Parcelable, Comparable<RadarDataBean> {
        public static final Parcelable.Creator<RadarDataBean> CREATOR = new Parcelable.Creator<RadarDataBean>() {
            @Override
            public RadarDataBean createFromParcel(Parcel parcel) {
                return new RadarDataBean(parcel);
            }

            @Override
            public RadarDataBean[] newArray(int i) {
                return new RadarDataBean[i];
            }
        };
        public static int size = 2;
        int distance;
        int speed;
        long timeStamp;

        @Override
        public int describeContents() {
            return 0;
        }

        public RadarDataBean(int i, int i2, long j) {
            this.distance = i2;
            this.speed = i;
            this.timeStamp = j;
        }

        public static RadarDataBean emptyData() {
            return new RadarDataBean(0, 0, 0L);
        }

        public int getDistance() {
            return this.distance;
        }

        public void setDistance(int i) {
            this.distance = i;
        }

        public int getSpeed() {
            return this.speed;
        }

        public void setSpeed(int i) {
            this.speed = i;
        }

        public long getTimeStamp() {
            return this.timeStamp;
        }

        public void setTimeStamp(long j) {
            this.timeStamp = j;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(this.distance);
            parcel.writeInt(this.speed);
            parcel.writeLong(this.timeStamp);
        }

        protected RadarDataBean(Parcel parcel) {
            this.distance = parcel.readInt();
            this.speed = parcel.readInt();
            this.timeStamp = parcel.readLong();
        }

        @Override
        public int compareTo(RadarDataBean radarDataBean) {
            return getDistance() - radarDataBean.getDistance();
        }
    }
}
